
"use strict"

angular.module('app', ['ngMaterial']).
    // factory('MesService', MesService).

    controller('demoCtrl', function ($scope, $http) {
        $scope.done_loading = true;  //初始化
        $scope.fileName = "";//上传文件名称
        $scope.filetype = "";
        $scope.new_fileName = "";//新文件名称
        //查询用户信息
        // MesService.userinfo().success(function (response) {
        //     if (response.user == null) {
        //         window.location.href = '../login.html?redirect=index.html';
        //     }
        //     else {
        //         $scope.userinfo = response;
        //
        //     }
        // });


        //获取用户登录状态
        $http({
            url: '../auth/userinfo',
            method: "GET",
            headers: {
                'Cache-Control': 'no-store',
                'Pragma': 'no-cache'
            }
        }).success(function (response) {

            if (response.user == null) {
                var defaultuser = getQueryString('defaultuser');
                if (defaultuser) {
                    //url中带入了默认用户信息，直接以默认用户登陆
                    $http({
                        url: '../app/login',
                        method: "POST",
                        data: {
                            'userName': 'JRIDGE',
                            'password': '123456'
                        },
                        headers: {
                            'Cache-Control': 'no-store',
                            'Pragma': 'no-cache'
                        }
                    }).success(function (response) {
                        //登陆成功,去获取当前用户
                        $http({
                            url: '../auth/userinfo',
                            method: "GET",
                            headers: {
                                'Cache-Control': 'no-store',
                                'Pragma': 'no-cache'
                            }
                        }).success(function (response) {
                            if (response.user != null) {
                                $scope.initdata(response);
                            }
                            else {
                                window.location.href = './login.html'
                            }
                        });
                    });
                }
                else {
                    window.location.href = './login.html'
                }
            } else {
                $scope.initdata(response);
            }
        })

        $scope.initdata = function (response) {
            $scope.userinfo = response;
        }





        //当前文件夹ID和上一级文件夹ID
        $scope.curID = '';
        $scope.parentID = '';

        //取根目录
        var url0 = '../folderlist/sublistpdm'
        $http({
            url: url0,
            method: 'GET',
            headers: {
                'Cache-Control': 'no-store',
                'Pragma': 'no-cache'
            }
        }).success(function (response) {
            $scope.folders = response;
            // for (var i = $scope.folders.legnth - 1; i >= 0; i--) {
            //     if (["PlmFolder", "MesFolder", "SysFolder"].indexOf($scope.folders[i].type)) {
            //         $scope.folders.splice(i, 1)
            //     }
            // }
            $scope.curID = 0;
        })


        //初始化
        $scope.notLastFolder = true;
        //进入文件夹
        $scope.folderId = "";
        //当前目录
        $scope.curFolderNames = [{
            "id": 0,
            "properties": {
                "type": "",
                "name": "根目录",
                "description": "",
                "creator": "",
                "creationdate": ""
            },
            "type": "PdmFolder"
        }]


        $scope.subfolder = function (list) {
            if (list.type == 'PdmFolder') {
                $scope.includePath = "./loading.html";
                //更新当前文件夹
                if (list.id !== $scope.curFolderNames[$scope.curFolderNames.length - 1].id) {
                    $scope.curFolderNames.push(list);
                }
                $scope.curID = $scope.curFolderNames.id;
                $http({
                    url: '../folderlist/sublistpdm?folderid=' + encodeURI(list.id),
                    method: "GET",
                    headers: {
                        'Cache-Control': 'no-store',
                        'Pragma': 'no-cache'
                    }
                }).success(function (response) {
                    $scope.folders = response;
                    // for (var i = $scope.folders.length - 1; i >= 0; i--) {
                    //     if (["PlmFolder", "MesFolder", "SysFolder"].indexOf($scope.folders[i].type)) {
                    //         $scope.folders.splice(i, 1)
                    //     }
                    // }
                    if ($scope.folders.length == 0) {
                        $scope.notLastFolder = false;
                    }
                });
            }
        }

        //退回上一级目录

        $scope.goBackToLastFolder = function () {
            //判断是否为最后一级目录，若为最后一级目录，则返回$scope.curFolderNames.length-1
            //若不为最后一级目录，则返回$scope.curFolderNames.length-2

            //判断是否为第一级目录，若为第一级，则返回上一级目录隐藏
            if ($scope.curFolderNames.length == 2) {
                $scope.curID = 0;
            }
            if ($scope.curFolderNames[$scope.curFolderNames.length - 2].id == 0) {
                var url = '../folderlist/sublistpdm'
            } else {
                var url = '../folderlist/sublistpdm?folderid=' + $scope.curFolderNames[$scope.curFolderNames.length - 2].id
            }
            $http({
                url: url,
                method: "GET",
                headers: {
                    'Cache-Control': 'no-store',
                    'Pragma': 'no-cache'
                }
            }).success(function (response) {
                $scope.folders = response;
                // for (var i = $scope.folders.length - 1; i >= 0; i--) {
                //     if (["PlmFolder", "MesFolder", "SysFolder"].indexOf($scope.folders[i].type)) {
                //         $scope.folders.splice(i, 1)
                //     }
                // }
                $scope.curFolderNames.splice($scope.curFolderNames.length - 1, 1);
                $scope.notLastFolder = true;
            });

        }

        //当前选择目录
        $scope.selFolderNames = [{
            "id": 0,
            "properties": {
                "type": "",
                "name": "根目录",
                "description": "",
                "creator": "",
                "creationdate": ""
            },
            "type": "PdmFolder"
        }]

        ///////////////////////////////////上传文件//////////////////////////////////////
        $scope.existingObj = "";
        //确认上传目录
        $scope.setFolder = function (index) {
            $scope.folderSelected = $scope.folders[index].properties.name;
            $scope.selFolderNames = $scope.curFolderNames;
            $scope.selFolderNames.push($scope.folders[index]); //将选择文件夹推送到当前文件夹对象中
            $scope.existingObj = $scope.selFolderNames[$scope.selFolderNames.length - 1];  //文件关联的文件夹
            $scope.parentObj = $scope.selFolderNames[$scope.selFolderNames.length - 1]; //文件上传的父文件夹
            console.log($scope.selFolderNames);
            $('#chooseFolder').modal('hide');
            console.log($scope.existingObj);
        }

        //上传文件成功后清除文件
        $scope.upload_success_close = function () {
            $('#upload_success').modal('hide');
            document.getElementById("i_select_files").style.display = "block"; //隐藏选择文件栏
            document.getElementById("previewPic").style.display = "none"; //显示预览框
        }

        $scope.rename = function () {
            $('#renameModal').modal('show');
            var temp = $scope.fileName.split(".");
            $scope.filetype = temp[temp.length - 1]
        }

        $scope.confirmRename = function () {
            $('#renameModal').modal('hide');
            $scope.fileName = $scope.new_fileName + "." + $scope.filetype;
            $scope.new_fileName = ""
        }

    });